import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.Scanner;

public class demo15 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader
                (new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        String[] str = reader.readLine().split(" ");
        String s1 = str[0], s2 = str[1];
        int prev1 = -1,prev2 = -1, ret = 0x3f3f3f3f;
        for (int i = 0;i < n;i++){
            String s = reader.readLine();
            if (s.equals(s1)){
                if (prev2 != -1){
                    ret = Math.min(ret,i - prev2);
                }
                prev1 = i;
            }else if (s.equals(s2)){
                ret = Math.min(ret,i - prev1);
            }
            prev2 = i;
        }
        System.out.println(ret == 0x3f3f3f3f ? -1 : ret);
    }
}
